<template>
  <div class="table-container">
    <div class="topDivBox">
      <el-form ref="form" :inline="true" :model="queryForm" @submit.native.prevent label-width="auto">
        <el-form-item label="区域:">
          <el-select v-model="queryForm.area" placeholder="区域">
            <el-option v-for="item in areaList" :key="item.id" :label="item.area" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="初高中:">
          <el-select v-model="queryForm.schoolType" placeholder="初高中">
            <el-option v-for="item in Dpmentlist" :key="item.value" :label="item.dpName" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学校类型:">
          <el-select v-model="queryForm.schoolTag" placeholder="学校类型" clearable>
            <el-option v-for="item in schoolTagList" :key="item.keyName" :label="item.memo" :value="item.keyName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学校名称:">
          <el-input v-model="queryForm.schoolName" placeholder="请输入学校名称查询" clearable />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" native-type="submit" type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
      <div><el-button icon="el-icon-plus" type="primary" @click="handleAdd">新增学校</el-button></div>
    </div>

    <el-table border ref="tableSort" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" :height="height">
      <el-table-column label="学校名称" prop="schoolName" show-overflow-tooltip min-width="20" align="center" />
      <el-table-column label="学校区域" prop="areaId" show-overflow-tooltip min-width="10" align="center">
        <template #default="{ row }">
          <el-tag type="info">{{ findArea(row.areaId) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="学校类型" prop="areaId" show-overflow-tooltip min-width="10">
        <template #default="{ row }">
          <!-- <el-tag :type="row.schoolType == 0 ? 'success' : 'primary'">
            {{ row.schoolType == 0 ? '初中' : '高中' }}
          </el-tag> -->

          <el-tag :type="getListTag(row.schoolTag)">
            {{ findSchoolTag(row.schoolTag) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="统招分数线" show-overflow-tooltip min-width="10">
        <template #default="{ row }">
          <el-tag type="warning" v-if="row.schoolType == 1">{{ row.unifiedScore }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="指标分数线" show-overflow-tooltip min-width="10">
        <template #default="{ row }">
          <el-tag type="warning" v-if="row.schoolType == 1">{{ row.targetScore }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip width="200" />
      <el-table-column label="更新时间" prop="updateTime" show-overflow-tooltip width="200" /> -->
      <el-table-column label="操作" show-overflow-tooltip width="250" align="center">
        <template #default="{ row }">
          <el-button type="warning" size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button type="success" size="small" v-if="row.schoolType == 0" @click="IndicatorTableEdit(row)">指标</el-button>
          <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[100, 200, 300, 400]"
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <table-edit @fetch-data="fetchData" ref="edit" />
    <IndicatorTable @fetch-data="fetchData" ref="IndicatorTable" />
  </div>
</template>

<script>
  import { GetDictListWithKeyName } from '@/api/table'
  import { GetAreaList } from '@/api/userManagement'
  import { GetSchoolList, DelSchool } from '@/api/user'
  import TableEdit from './components/TableEdit'
  import IndicatorTable from './components/IndicatorTable'

  export default {
    name: 'ComprehensiveTable',
    components: {
      TableEdit,
      IndicatorTable,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        Dpmentlist: [
          { dpName: '全部', value: 2 },
          { dpName: '初中', value: 0 },
          { dpName: '高中', value: 1 },
        ],
        schoolTagList: [],
        areaList: [{ area: '全部', id: 0 }],
        imgShow: true,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 100,
          schoolName: '',
          schoolType: 2,
          area: 0,
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    async created() {
      const { data } = await GetAreaList()
      const data1 = await GetDictListWithKeyName({ keyName: '' })
      this.schoolTagList = data1.data.filter((item) => [1, 2].includes(item.disClass))

      this.areaList = [...this.areaList, ...data]

      this.fetchData()
    },
    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    mounted() {},
    methods: {
      getListTag(tag) {
        if (this.schoolTagList[0]) {
          if (this.schoolTagList[0].keyName == tag || this.schoolTagList[3].keyName == tag) {
            return 'success'
          } else if (this.schoolTagList[1].keyName == tag || this.schoolTagList[4].keyName == tag) {
            return 'primary'
          } else {
            return 'danger'
          }
        } else {
          return 'info'
        }
      },
      findSchoolTag(tag) {
        return this.schoolTagList.find((item) => item.keyName == tag).memo
      },
      IndicatorTableEdit(row) {
        this.$refs['IndicatorTable'].showEdit(row)
      },
      findArea(id) {
        return this.areaList.find((item) => item.id === id).area
      },
      getType(time1, time2) {
        return time1.slice(0, 10) === time2.slice(0, 10)
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗，有可能会影响数据，请谨慎删除', null, async () => {
          const { msg } = await DelSchool({ id: row.id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },

      async fetchData() {
        this.listLoading = true
        let box = Object.assign({}, this.queryForm)
        const { data } = await GetSchoolList(box)
        this.list = data.records
        this.total = data.total
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>

<style scoped lang="scss">
  .topDivBox {
    display: flex;
    justify-content: space-between;
    align-content: center;
  }
  .booksinfo {
    display: flex;
    .bookname {
      margin-left: 10px;
      font-size: 700;
    }
    .bookauthor {
      margin-left: 10px;
    }
  }
</style>
